home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 24 / CU Amiga Magazine's Super CD-ROM 24 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-07].iso / CUCD / Utilities / vim-5.1 / src / buffer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-28  |  52.1 KB  |  2,267 lines

  1. /* vi:set ts=8 sts=4 sw=4:
  2.  *
  3.  * VIM - Vi IMproved    by Bram Moolenaar
  4.  *
  5.  * Do ":help uganda"  in Vim to read copying and usage conditions.
  6.  * Do ":help credits" in Vim to see a list of people who contributed.
  7.  */
  8.  
  9. /*
  10.  * buffer.c: functions for dealing with the buffer structure
  11.  */
  12.  
  13. /*
  14.  * The buffer list is a double linked list of all buffers.
  15.  * Each buffer can be in one of these states:
  16.  * never loaded: BF_NEVERLOADED is set, only the file name is valid
  17.  *   not loaded: b_ml.ml_mfp == NULL, no memfile allocated
  18.  *     hidden: b_nwindows == 0, loaded but not displayed in a window
  19.  *     normal: loaded and displayed in a window
  20.  *
  21.  * Instead of storing file names all over the place, each file name is
  22.  * stored in the buffer list. It can be referenced by a number.
  23.  *
  24.  * The current implementation remembers all file names ever used.
  25.  */
  26.  
  27. #include "vim.h"
  28.  
  29. static void    enter_buffer __ARGS((BUF *));
  30. static char_u    *buflist_match __ARGS((vim_regexp *prog, BUF *buf));
  31. static char_u    *buflist_match_try __ARGS((vim_regexp *prog, char_u *name));
  32. static void    buflist_setlnum __ARGS((BUF *, linenr_t, colnr_t));
  33. static WINLNUM    *buflist_findwlp __ARGS((BUF *buf));
  34. static int    append_arg_number __ARGS((char_u *, int, int));
  35. static void    free_buffer __ARGS((BUF *));
  36.  
  37. /*
  38.  * Open current buffer, that is: open the memfile and read the file into memory
  39.  * return FAIL for failure, OK otherwise
  40.  */
  41.     int
  42. open_buffer(read_stdin)
  43.     int        read_stdin;        /* read file from stdin */
  44. {
  45.     int        retval = OK;
  46. #ifdef AUTOCMD
  47.     BUF        *old_curbuf;
  48.     BUF        *new_curbuf;
  49. #endif
  50.  
  51.     /*
  52.      * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
  53.      * When re-entering the same buffer, it should not change, because the
  54.      * user may have reset the flag by hand.
  55.      */
  56.     if (readonlymode && curbuf->b_ffname != NULL
  57.                     && (curbuf->b_flags & BF_NEVERLOADED))
  58.     curbuf->b_p_ro = TRUE;
  59.  
  60.     if (ml_open() == FAIL)
  61.     {
  62.     /*
  63.      * There MUST be a memfile, otherwise we can't do anything
  64.      * If we can't create one for the current buffer, take another buffer
  65.      */
  66.     close_buffer(NULL, curbuf, FALSE, FALSE);
  67.     for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
  68.         if (curbuf->b_ml.ml_mfp != NULL)
  69.         break;
  70.     /*
  71.      * if there is no memfile at all, exit
  72.      * This is OK, since there are no changes to loose.
  73.      */
  74.     if (curbuf == NULL)
  75.     {
  76.         EMSG("Cannot allocate buffer, exiting...");
  77.         getout(2);
  78.     }
  79.     EMSG("Cannot allocate buffer, using other one...");
  80.     enter_buffer(curbuf);
  81.     return FAIL;
  82.     }
  83. #ifdef AUTOCMD
  84.     /* The autocommands in readfile() may change the buffer, but only AFTER
  85.      * reading the file. */
  86.     old_curbuf = curbuf;
  87.     modified_was_set = FALSE;
  88. #endif
  89.     if (curbuf->b_ffname != NULL)
  90.     retval = readfile(curbuf->b_ffname, curbuf->b_fname,
  91.                (linenr_t)0, (linenr_t)0, (linenr_t)MAXLNUM, READ_NEW);
  92.     else if (read_stdin)
  93.     retval = readfile(NULL, NULL, (linenr_t)0,
  94.                (linenr_t)0, (linenr_t)MAXLNUM, READ_NEW + READ_STDIN);
  95.     else
  96.     {
  97.     MSG("Empty Buffer");
  98.     msg_col = 0;
  99.     msg_didout = FALSE;    /* overwrite this message whenever you like */
  100.     }
  101.  
  102.     /* if first time loading this buffer, init chartab */
  103.     if (curbuf->b_flags & BF_NEVERLOADED)
  104.     init_chartab();
  105.  
  106.     /*
  107.      * Set/reset the Changed flag first, autocmds may change the buffer.
  108.      * Apply the automatic commands, before processing the modelines.
  109.      * So the modelines have priority over auto commands.
  110.      */
  111.     /* When reading stdin, the buffer contents always needs writing, so set
  112.      * the changed flag.  Unless in readonly mode: "ls | gview -". */
  113.     if ((read_stdin && !readonlymode)
  114. #ifdef AUTOCMD
  115.         || modified_was_set    /* ":set modified" used in autocmd */
  116. #endif
  117.         )
  118.     CHANGED;
  119.     else if (retval != FAIL)
  120.     UNCHANGED(curbuf);
  121.     curbuf->b_start_ffc = *curbuf->b_p_ff;    /* keep this fileformat */
  122.  
  123.     /* require "!" to overwrite the file, because it wasn't read completely */
  124.     if (got_int)
  125.     curbuf->b_notedited = TRUE;
  126.  
  127. #ifdef AUTOCMD
  128.     apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE);
  129. #endif
  130.  
  131.     if (retval != FAIL)
  132.     {
  133. #ifdef AUTOCMD
  134.     /*
  135.      * The autocommands may have changed the current buffer.  Apply the
  136.      * modelines to the correct buffer, if it still exists.
  137.      */
  138.     if (buf_valid(old_curbuf))
  139.     {
  140.         new_curbuf = curbuf;
  141.         curbuf = old_curbuf;
  142.         curwin->w_buffer = old_curbuf;
  143. #endif
  144.         do_modelines();
  145.         curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
  146. #ifdef AUTOCMD
  147.         curbuf = new_curbuf;
  148.         curwin->w_buffer = new_curbuf;
  149.     }
  150. #endif
  151.     }
  152.  
  153.     return retval;
  154. }
  155.  
  156. /*
  157.  * Return TRUE if "buf" points to a valid buffer (in the buffer list).
  158.  */
  159.     int
  160. buf_valid(buf)
  161.     BUF        *buf;
  162. {
  163.     BUF        *bp;
  164.  
  165.     for (bp = firstbuf; bp != NULL; bp = bp->b_next)
  166.     if (bp == buf)
  167.         return TRUE;
  168.     return FALSE;
  169. }
  170.  
  171. /*
  172.  * Close the link to a buffer. If "free_buf" is TRUE free the buffer if it
  173.  * becomes unreferenced. The caller should get a new buffer very soon!
  174.  * if 'del_buf' is TRUE, remove the buffer from the buffer list.
  175.  */
  176.     void
  177. close_buffer(win, buf, free_buf, del_buf)
  178.     WIN        *win;        /* if not NULL, set b_last_cursor */
  179.     BUF        *buf;
  180.     int        free_buf;
  181.     int        del_buf;
  182. {
  183.     if (buf->b_nwindows > 0)
  184.     --buf->b_nwindows;
  185.     if (buf->b_nwindows == 0 && win != NULL)
  186.     set_last_cursor(win);    /* may set b_last_cursor */
  187.     if (buf->b_nwindows > 0 || !free_buf)
  188.     {
  189.     if (buf == curbuf)
  190.         u_sync();        /* sync undo before going to another buffer */
  191.     return;
  192.     }
  193.  
  194.     /* Always remove the buffer when there is no file name. */
  195.     if (buf->b_ffname == NULL)
  196.     del_buf = TRUE;
  197.  
  198.     /*
  199.      * Free all things allocated for this buffer.
  200.      * Also calls the "BufDelete" autocommands when del_buf is TRUE.
  201.      */
  202.     buf_freeall(buf, del_buf);
  203.     if (!buf_valid(buf))        /* autocommands may delete the buffer */
  204.     return;
  205.  
  206.     /*
  207.      * Remove the buffer from the list.
  208.      */
  209.     if (del_buf)
  210.     {
  211.     vim_free(buf->b_ffname);
  212.     vim_free(buf->b_sfname);
  213.     if (buf->b_prev == NULL)
  214.         firstbuf = buf->b_next;
  215.     else
  216.         buf->b_prev->b_next = buf->b_next;
  217.     if (buf->b_next == NULL)
  218.         lastbuf = buf->b_prev;
  219.     else
  220.         buf->b_next->b_prev = buf->b_prev;
  221.     free_buffer(buf);
  222.     }
  223.     else
  224.     buf_clear(buf);
  225. }
  226.  
  227. /*
  228.  * buf_clear() - make buffer empty
  229.  */
  230.     void
  231. buf_clear(buf)
  232.     BUF        *buf;
  233. {
  234.     buf->b_ml.ml_line_count = 1;
  235.     buf->b_changed = FALSE;
  236.     buf->b_start_ffc = *buf->b_p_ff;
  237. #ifndef SHORT_FNAME
  238.     buf->b_shortname = FALSE;
  239. #endif
  240.     buf->b_p_eol = TRUE;
  241.     buf->b_ml.ml_mfp = NULL;
  242.     buf->b_ml.ml_flags = ML_EMPTY;        /* empty buffer */
  243. }
  244.  
  245. /*
  246.  * buf_freeall() - free all things allocated for the buffer
  247.  */
  248.     void
  249. buf_freeall(buf, del_buf)
  250.     BUF        *buf;
  251.     int        del_buf;    /* buffer is going to be deleted */
  252. {
  253. #ifdef AUTOCMD
  254.     apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE);
  255.     if (!buf_valid(buf))        /* autocommands may delete the buffer */
  256.     return;
  257.     if (del_buf)
  258.     {
  259.     apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE);
  260.     if (!buf_valid(buf))        /* autocommands may delete the buffer */
  261.         return;
  262.     }
  263. #endif
  264.     u_blockfree(buf);            /* free the memory allocated for undo */
  265.     ml_close(buf, TRUE);        /* close and delete the memline/memfile */
  266.     buf->b_ml.ml_line_count = 0;    /* no lines in buffer */
  267.     u_clearall(buf);            /* reset all undo information */
  268. #ifdef SYNTAX_HL
  269.     syntax_clear(buf);            /* reset syntax info */
  270. #endif
  271. #ifdef WANT_EVAL
  272.     var_clear(&buf->b_vars);        /* free all internal variables */
  273. #endif
  274. }
  275.  
  276. /*
  277.  * Free a buffer structure and some of the things it contains.
  278.  */
  279.     static void
  280. free_buffer(buf)
  281.     BUF        *buf;
  282. {
  283.     WINLNUM    *wlp;
  284.  
  285.     /* Free the b_winlnum list for buffer "buf". */
  286.     while (buf->b_winlnum != NULL)
  287.     {
  288.     wlp = buf->b_winlnum;
  289.     buf->b_winlnum = wlp->wl_next;
  290.     vim_free(wlp);
  291.     }
  292.  
  293. #ifdef HAVE_PERL_INTERP
  294.     perl_buf_free(buf);
  295. #endif
  296. #ifdef HAVE_PYTHON
  297.     python_buffer_free(buf);
  298. #endif
  299.  
  300.     free_buf_options(buf, TRUE);
  301.     vim_free(buf);
  302. }
  303.  
  304. /*
  305.  * do_bufdel() - delete or unload buffer(s)
  306.  *
  307.  * addr_count == 0: ":bdel" - delete current buffer
  308.  * addr_count == 1: ":N bdel" or ":bdel N [N ..] - first delete
  309.  *            buffer "end_bnr", then any other arguments.
  310.  * addr_count == 2: ":N,N bdel" - delete buffers in range
  311.  *
  312.  * command can be DOBUF_UNLOAD (":bunload") or DOBUF_DEL (":bdel")
  313.  *
  314.  * Returns error message or NULL
  315.  */
  316.     char_u *
  317. do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
  318.     int        command;
  319.     char_u  *arg;    /* pointer to extra arguments */
  320.     int        addr_count;
  321.     int        start_bnr;    /* first buffer number in a range */
  322.     int        end_bnr;    /* buffer number or last buffer number in a range */
  323.     int        forceit;
  324. {
  325.     int        do_current = 0;    /* delete current buffer? */
  326.     int        deleted = 0;    /* number of buffers deleted */
  327.     char_u  *errormsg = NULL;    /* return value */
  328.     int        bnr;        /* buffer number */
  329.     char_u  *p;
  330.  
  331.     if (addr_count == 0)
  332.     (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
  333.     else
  334.     {
  335.     if (addr_count == 2)
  336.     {
  337.         if (*arg)        /* both range and argument is not allowed */
  338.         return e_trailing;
  339.         bnr = start_bnr;
  340.     }
  341.     else    /* addr_count == 1 */
  342.         bnr = end_bnr;
  343.  
  344.     for ( ;!got_int; ui_breakcheck())
  345.     {
  346.         /*
  347.          * delete the current buffer last, otherwise when the
  348.          * current buffer is deleted, the next buffer becomes
  349.          * the current one and will be loaded, which may then
  350.          * also be deleted, etc.
  351.          */
  352.         if (bnr == curbuf->b_fnum)
  353.         do_current = bnr;
  354.         else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
  355.                                    forceit) == OK)
  356.         ++deleted;
  357.  
  358.         /*
  359.          * find next buffer number to delete/unload
  360.          */
  361.         if (addr_count == 2)
  362.         {
  363.         if (++bnr > end_bnr)
  364.             break;
  365.         }
  366.         else    /* addr_count == 1 */
  367.         {
  368.         arg = skipwhite(arg);
  369.         if (*arg == NUL)
  370.             break;
  371.         if (!isdigit(*arg))
  372.         {
  373.             p = skiptowhite_esc(arg);
  374.             bnr = buflist_findpat(arg, p);
  375.             if (bnr < 0)        /* failed */
  376.             break;
  377.             arg = p;
  378.         }
  379.         else
  380.             bnr = getdigits(&arg);
  381.         }
  382.     }
  383.     if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
  384.                       FORWARD, do_current, forceit) == OK)
  385.         ++deleted;
  386.  
  387.     if (deleted == 0)
  388.     {
  389.         sprintf((char *)IObuff, "No buffers were %s",
  390.             command == DOBUF_UNLOAD ? "unloaded" : "deleted");
  391.         errormsg = IObuff;
  392.     }
  393.     else
  394.         smsg((char_u *)"%d buffer%s %s", deleted,
  395.             plural((long)deleted),
  396.             command == DOBUF_UNLOAD ? "unloaded" : "deleted");
  397.     }
  398.  
  399.     return errormsg;
  400. }
  401.  
  402. /*
  403.  * Implementation of the command for the buffer list
  404.  *
  405.  * action == DOBUF_GOTO        go to specified buffer
  406.  * action == DOBUF_SPLIT    split window and go to specified buffer
  407.  * action == DOBUF_UNLOAD   unload specified buffer(s)
  408.  * action == DOBUF_DEL        delete specified buffer(s)
  409.  *
  410.  * start == DOBUF_CURRENT   go to "count" buffer from current buffer
  411.  * start == DOBUF_FIRST        go to "count" buffer from first buffer
  412.  * start == DOBUF_LAST        go to "count" buffer from last buffer
  413.  * start == DOBUF_MOD        go to "count" modified buffer from current buffer
  414.  *
  415.  * Return FAIL or OK.
  416.  */
  417.     int
  418. do_buffer(action, start, dir, count, forceit)
  419.     int        action;
  420.     int        start;
  421.     int        dir;    /* FORWARD or BACKWARD */
  422.     int        count;    /* buffer number or number of buffers */
  423.     int        forceit;    /* TRUE for :...! */
  424. {
  425.     BUF        *buf;
  426.     BUF        *delbuf;
  427.     int        retval;
  428.     int        forward;
  429.  
  430.     switch (start)
  431.     {
  432.     case DOBUF_FIRST:   buf = firstbuf; break;
  433.     case DOBUF_LAST:    buf = lastbuf;  break;
  434.     default:        buf = curbuf;   break;
  435.     }
  436.     if (start == DOBUF_MOD)        /* find next modified buffer */
  437.     {
  438.     while (count-- > 0)
  439.     {
  440.         do
  441.         {
  442.         buf = buf->b_next;
  443.         if (buf == NULL)
  444.             buf = firstbuf;
  445.         }
  446.         while (buf != curbuf && !buf_changed(buf));
  447.     }
  448.     if (!buf_changed(buf))
  449.     {
  450.         EMSG("No modified buffer found");
  451.         return FAIL;
  452.     }
  453.     }
  454.     else if (start == DOBUF_FIRST && count) /* find specified buffer number */
  455.     {
  456.     while (buf != NULL && buf->b_fnum != count)
  457.         buf = buf->b_next;
  458.     }
  459.     else
  460.     {
  461.     while (count-- > 0)
  462.     {
  463.         if (dir == FORWARD)
  464.         {
  465.         buf = buf->b_next;
  466.         if (buf == NULL)
  467.             buf = firstbuf;
  468.         }
  469.         else
  470.         {
  471.         buf = buf->b_prev;
  472.         if (buf == NULL)
  473.             buf = lastbuf;
  474.         }
  475.     }
  476.     }
  477.  
  478.     if (buf == NULL)        /* could not find it */
  479.     {
  480.     if (start == DOBUF_FIRST)
  481.     {
  482.                         /* don't warn when deleting */
  483.         if (action != DOBUF_UNLOAD && action != DOBUF_DEL)
  484.         EMSGN("Cannot go to buffer %ld", count);
  485.     }
  486.     else if (dir == FORWARD)
  487.         EMSG("Cannot go beyond last buffer");
  488.     else
  489.         EMSG("Cannot go before first buffer");
  490.     return FAIL;
  491.     }
  492.  
  493.     /*
  494.      * delete buffer buf from memory and/or the list
  495.      */
  496.     if (action == DOBUF_UNLOAD || action == DOBUF_DEL)
  497.     {
  498.     if (!forceit && buf_changed(buf))
  499.     {
  500.         EMSGN("No write since last change for buffer %ld (use ! to override)",
  501.             buf->b_fnum);
  502.         return FAIL;
  503.     }
  504.  
  505.     /*
  506.      * If deleting last buffer, make it empty.
  507.      * The last buffer cannot be unloaded.
  508.      */
  509.     if (firstbuf->b_next == NULL)
  510.     {
  511.         if (action == DOBUF_UNLOAD)
  512.         {
  513.         EMSG("Cannot unload last buffer");
  514.         return FAIL;
  515.         }
  516.  
  517.         /* Close any other windows on this buffer, then make it empty. */
  518.         close_others(FALSE, TRUE);
  519.         buf = curbuf;
  520.         setpcmark();
  521.         retval = do_ecmd(0, NULL, NULL, NULL, (linenr_t)1,
  522.                           forceit ? ECMD_FORCEIT : 0);
  523.  
  524.         /*
  525.          * do_ecmd() may create a new buffer, then we have to delete
  526.          * the old one.  But do_ecmd() may have done that already, check
  527.          * if the buffer still exists.
  528.          */
  529.         if (buf != curbuf && buf_valid(buf))
  530.         close_buffer(NULL, buf, TRUE, TRUE);
  531.         return retval;
  532.     }
  533.  
  534.     /*
  535.      * If the deleted buffer is the current one, close the current window
  536.      * (unless it's the only window).
  537.      */
  538.     while (buf == curbuf && firstwin != lastwin)
  539.         close_window(curwin, FALSE);
  540.  
  541.     /*
  542.      * If the buffer to be deleted is not current one, delete it here.
  543.      */
  544.     if (buf != curbuf)
  545.     {
  546.         close_windows(buf);
  547.         if (buf_valid(buf))
  548.         close_buffer(NULL, buf, TRUE, action == DOBUF_DEL);
  549.         return OK;
  550.     }
  551.  
  552.     /*
  553.      * Deleting the current buffer: Need to find another buffer to go to.
  554.      * There must be another, otherwise it would have been handled above.
  555.      * First try to find one that is loaded, after the current buffer,
  556.      * then before the current buffer.
  557.      */
  558.     forward = TRUE;
  559.     buf = curbuf->b_next;
  560.     for (;;)
  561.     {
  562.         if (buf == NULL)
  563.         {
  564.         if (!forward)    /* tried both directions */
  565.             break;
  566.         buf = curbuf->b_prev;
  567.         forward = FALSE;
  568.         continue;
  569.         }
  570.         if (buf->b_ml.ml_mfp != NULL)
  571.         break;
  572.         if (forward)
  573.         buf = buf->b_next;
  574.         else
  575.         buf = buf->b_prev;
  576.     }
  577.     if (buf == NULL)    /* No loaded buffers, just take one */
  578.     {
  579.         if (curbuf->b_next != NULL)
  580.         buf = curbuf->b_next;
  581.         else
  582.         buf = curbuf->b_prev;
  583.     }
  584.     }
  585.  
  586.     /*
  587.      * make buf current buffer
  588.      */
  589.     if (action == DOBUF_SPLIT)        /* split window first */
  590.     {
  591.     if (win_split(0, FALSE, FALSE) == FAIL)
  592.         return FAIL;
  593.     }
  594.  
  595.     /* go to current buffer - nothing to do */
  596.     if (buf == curbuf)
  597.     return OK;
  598.  
  599.     /*
  600.      * Check if the current buffer may be abandoned.
  601.      */
  602.     if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
  603.     {
  604.     EMSG(e_nowrtmsg);
  605.     return FAIL;
  606.     }
  607.  
  608.     setpcmark();
  609.     curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
  610.     buflist_altlnum();             /* remember curpos.lnum */
  611.  
  612.     /* close_windows() or apply_autocmds() may change curbuf */
  613.     delbuf = curbuf;
  614.  
  615. #ifdef AUTOCMD
  616.     apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE);
  617.     if (buf_valid(delbuf))
  618. #endif
  619.     {
  620.     if (action == DOBUF_UNLOAD || action == DOBUF_DEL)
  621.         close_windows(delbuf);
  622.     if (buf_valid(delbuf))
  623.         close_buffer(delbuf == curwin->w_buffer ? curwin : NULL, delbuf,
  624.             (action == DOBUF_GOTO && !p_hid && !buf_changed(delbuf))
  625.                  || action == DOBUF_UNLOAD || action == DOBUF_DEL,
  626.               action == DOBUF_DEL);
  627.     }
  628. #ifdef AUTOCMD
  629.     if (buf_valid(buf))        /* an autocommand may have deleted buf! */
  630. #endif
  631.     enter_buffer(buf);
  632.     return OK;
  633. }
  634.  
  635. /*
  636.  * enter a new current buffer.
  637.  * (old curbuf must have been freed already)
  638.  */
  639.     static void
  640. enter_buffer(buf)
  641.     BUF        *buf;
  642. {
  643.     buf_copy_options(curbuf, buf, BCO_ENTER | BCO_NOHELP);
  644.     curwin->w_buffer = buf;
  645.     curbuf = buf;
  646.     ++curbuf->b_nwindows;
  647.     if (curbuf->b_ml.ml_mfp == NULL)    /* need to load the file */
  648.     open_buffer(FALSE);
  649.     else
  650.     {
  651.     need_fileinfo = TRUE;        /* display file info after redraw */
  652.     buf_check_timestamp(curbuf);    /* check if file has changed */
  653. #ifdef AUTOCMD
  654.     apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE);
  655. #endif
  656.     }
  657.     buflist_getlnum();            /* restore curpos.lnum */
  658.     check_arg_idx();            /* check for valid arg_idx */
  659.     maketitle();
  660.     scroll_cursor_halfway(FALSE);    /* redisplay at correct position */
  661.     update_screen(NOT_VALID);
  662. }
  663.  
  664. /*
  665.  * functions for dealing with the buffer list
  666.  */
  667.  
  668. /*
  669.  * Add a file name to the buffer list. Return a pointer to the buffer.
  670.  * If the same file name already exists return a pointer to that buffer.
  671.  * If it does not exist, or if fname == NULL, a new entry is created.
  672.  * If use_curbuf is TRUE, may use current buffer.
  673.  * This is the ONLY way to create a new buffer.
  674.  */
  675. static int  top_file_num = 1;        /* highest file number */
  676.  
  677.     BUF *
  678. buflist_new(ffname, sfname, lnum, use_curbuf)
  679.     char_u    *ffname;
  680.     char_u    *sfname;
  681.     linenr_t    lnum;
  682.     int        use_curbuf;
  683. {
  684.     BUF        *buf;
  685.  
  686.     fname_expand(&ffname, &sfname);    /* will allocate ffname */
  687.  
  688.     /*
  689.      * If file name already exists in the list, update the entry.
  690.      */
  691.     if (ffname != NULL && (buf = buflist_findname(ffname)) != NULL)
  692.     {
  693.     vim_free(ffname);
  694.     if (lnum != 0)
  695.         buflist_setlnum(buf, lnum, 0);
  696.     /* copy the options now, if 'cpo' doesn't have 's' and not done
  697.      * already */
  698.     buf_copy_options(curbuf, buf, 0);
  699.     return buf;
  700.     }
  701.  
  702.     /*
  703.      * If the current buffer has no name and no contents, use the current
  704.      * buffer.    Otherwise: Need to allocate a new buffer structure.
  705.      *
  706.      * This is the ONLY place where a new buffer structure is allocated!
  707.      */
  708.     if (use_curbuf && curbuf != NULL && curbuf->b_ffname == NULL &&
  709.         curbuf->b_nwindows <= 1 &&
  710.         (curbuf->b_ml.ml_mfp == NULL || bufempty()))
  711.     buf = curbuf;
  712.     else
  713.     {
  714.     buf = (BUF *)alloc_clear((unsigned)sizeof(BUF));
  715.     if (buf == NULL)
  716.     {
  717.         vim_free(ffname);
  718.         return NULL;
  719.     }
  720.     }
  721.  
  722.     if (ffname != NULL)
  723.     {
  724.     buf->b_ffname = ffname;
  725.     buf->b_sfname = vim_strsave(sfname);
  726.     }
  727.     if (buf->b_winlnum == NULL)
  728.     buf->b_winlnum = (WINLNUM *)alloc((unsigned)sizeof(WINLNUM));
  729.     if ((ffname != NULL && (buf->b_ffname == NULL ||
  730.              buf->b_sfname == NULL)) || buf->b_winlnum == NULL)
  731.     {
  732.     vim_free(buf->b_ffname);
  733.     buf->b_ffname = NULL;
  734.     vim_free(buf->b_sfname);
  735.     buf->b_sfname = NULL;
  736.     if (buf != curbuf)
  737.         free_buffer(buf);
  738.     return NULL;
  739.     }
  740.  
  741.     if (buf == curbuf)
  742.     {
  743.     buf_freeall(buf, FALSE); /* free all things allocated for this buffer */
  744.     buf->b_nwindows = 0;
  745.     }
  746.     else
  747.     {
  748.     /*
  749.      * put new buffer at the end of the buffer list
  750.      */
  751.     buf->b_next = NULL;
  752.     if (firstbuf == NULL)        /* buffer list is empty */
  753.     {
  754.         buf->b_prev = NULL;
  755.         firstbuf = buf;
  756.     }
  757.     else                /* append new buffer at end of list */
  758.     {
  759.         lastbuf->b_next = buf;
  760.         buf->b_prev = lastbuf;
  761.     }
  762.     lastbuf = buf;
  763.  
  764.     buf->b_fnum = top_file_num++;
  765.     if (top_file_num < 0)        /* wrap around (may cause duplicates) */
  766.     {
  767.         EMSG("Warning: List of file names overflow");
  768.         ui_delay(3000L, TRUE);    /* make sure it is noticed */
  769.         top_file_num = 1;
  770.     }
  771.  
  772.     buf->b_winlnum->wl_lnum = lnum;
  773.     buf->b_winlnum->wl_col = 0;
  774.     buf->b_winlnum->wl_next = NULL;
  775.     buf->b_winlnum->wl_prev = NULL;
  776.     buf->b_winlnum->wl_win = curwin;
  777.  
  778. #ifdef WANT_EVAL
  779.     var_init(&buf->b_vars);        /* init internal variables */
  780. #endif
  781.     /*
  782.      * Always copy the options from the current buffer.
  783.      */
  784.     buf_copy_options(curbuf, buf, BCO_ALWAYS);
  785.     }
  786.  
  787.     buf->b_fname = buf->b_sfname;
  788.     buf->b_u_synced = TRUE;
  789.     buf->b_flags |= BF_CHECK_RO | BF_NEVERLOADED;
  790.     buf_clear(buf);
  791.     clrallmarks(buf);            /* clear marks */
  792.     fmarks_check_names(buf);        /* check file marks for this file */
  793.  
  794.     return buf;
  795. }
  796.  
  797. /*
  798.  * Get the highest buffer number.  Note that some buffers may have been
  799.  * deleted.
  800.  */
  801.     int
  802. buflist_maxbufnr()
  803. {
  804.     return (top_file_num - 1);
  805. }
  806.  
  807. /*
  808.  * Free the memory for the options of a buffer.
  809.  */
  810.     void
  811. free_buf_options(buf, free_p_ff)
  812.     BUF        *buf;
  813.     int        free_p_ff;        /* also free 'fileformat'? */
  814. {
  815.     if (free_p_ff)
  816.     free_string_option(buf->b_p_ff);
  817.     free_string_option(buf->b_p_fo);
  818.     free_string_option(buf->b_p_isk);
  819.     free_string_option(buf->b_p_com);
  820.     free_string_option(buf->b_p_nf);
  821. #ifdef CINDENT
  822.     free_string_option(buf->b_p_cink);
  823.     free_string_option(buf->b_p_cino);
  824. #endif
  825. #if defined(CINDENT) || defined(SMARTINDENT)
  826.     free_string_option(buf->b_p_cinw);
  827. #endif
  828. #ifdef INSERT_EXPAND
  829.     free_string_option(buf->b_p_cpt);
  830. #endif
  831. }
  832.  
  833. /*
  834.  * get alternate file n
  835.  * set linenr to lnum or altlnum if lnum == 0
  836.  * if (options & GETF_SETMARK) call setpcmark()
  837.  * if (options & GETF_ALT) we are jumping to an alternate file.
  838.  *
  839.  * return FAIL for failure, OK for success
  840.  */
  841.     int
  842. buflist_getfile(n, lnum, options, forceit)
  843.     int        n;
  844.     linenr_t    lnum;
  845.     int        options;
  846.     int        forceit;
  847. {
  848.     BUF        *buf;
  849.  
  850.     buf = buflist_findnr(n);
  851.     if (buf == NULL)
  852.     {
  853.     if ((options & GETF_ALT) && n == 0)
  854.         emsg(e_noalt);
  855.     else
  856.         EMSGN("buffer %ld not found", n);
  857.     return FAIL;
  858.     }
  859.  
  860.     /* if alternate file is the current buffer, nothing to do */
  861.     if (buf == curbuf)
  862.     return OK;
  863.  
  864.     /* altlnum may be changed by getfile(), get it now */
  865.     if (lnum == 0)
  866.     lnum = buflist_findlnum(buf);
  867.     ++RedrawingDisabled;
  868.     if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
  869.                               lnum, forceit) <= 0)
  870.     {
  871.     --RedrawingDisabled;
  872.     return OK;
  873.     }
  874.     --RedrawingDisabled;
  875.     return FAIL;
  876. }
  877.  
  878. /*
  879.  * go to the last know line number for the current buffer
  880.  */
  881.     void
  882. buflist_getlnum()
  883. {
  884.     WINLNUM    *wlp;
  885.  
  886.     curwin->w_cursor.lnum = 1;
  887.     curwin->w_cursor.col = 0;
  888.     wlp = buflist_findwlp(curbuf);
  889.     if (wlp != NULL && wlp->wl_lnum != 0
  890.                 && wlp->wl_lnum <= curbuf->b_ml.ml_line_count)
  891.     {
  892.     curwin->w_cursor.lnum = wlp->wl_lnum;
  893.     curwin->w_cursor.col = wlp->wl_col;
  894.     adjust_cursor();
  895.     }
  896. }
  897.  
  898. /*
  899.  * find file in buffer list by name (it has to be for the current window)
  900.  * 'ffname' must have a full path.
  901.  */
  902.     BUF *
  903. buflist_findname(ffname)
  904.     char_u    *ffname;
  905. {
  906.     BUF        *buf;
  907.  
  908.     for (buf = firstbuf; buf != NULL; buf = buf->b_next)
  909.     if (buf->b_ffname != NULL && fnamecmp(ffname, buf->b_ffname) == 0)
  910.         return (buf);
  911.     return NULL;
  912. }
  913.  
  914. /*
  915.  * Find file in buffer list by a regexppattern.
  916.  * Return fnum of the found buffer, < 0 for error.
  917.  */
  918.     int
  919. buflist_findpat(pattern, pattern_end)
  920.     char_u    *pattern;
  921.     char_u    *pattern_end;        /* pointer to first char after pattern */
  922. {
  923.     BUF        *buf;
  924.     vim_regexp    *prog;
  925.     int        fnum = -1;
  926.     char_u    *pat;
  927.     char_u    *match;
  928.     int        attempt;
  929.     char_u    *p;
  930.  
  931.     if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
  932.     {
  933.     if (*pattern == '%')
  934.         fnum = curbuf->b_fnum;
  935.     else
  936.         fnum = curwin->w_alt_fnum;
  937.     }
  938.  
  939.     /*
  940.      * Try four ways of matching:
  941.      * attempt == 0: without '^' or '$' (at any position)
  942.      * attempt == 1: with '^' at start (only at postion 0)
  943.      * attempt == 2: with '$' at end (only match at end)
  944.      * attempt == 3: with '^' at start and '$' at end (only full match)
  945.      */
  946.     else for (attempt = 0; attempt <= 3; ++attempt)
  947.     {
  948.     /* may add '^' and '$' */
  949.     pat = file_pat_to_reg_pat(pattern, pattern_end, NULL);
  950.     if (pat == NULL)
  951.         return -1;
  952.     if (attempt < 2)
  953.     {
  954.         p = pat + STRLEN(pat) - 1;
  955.         if (p > pat && *p == '$')            /* remove '$' */
  956.         *p = NUL;
  957.     }
  958.     p = pat;
  959.     if (*p == '^' && !(attempt & 1))        /* remove '^' */
  960.         ++p;
  961.     prog = vim_regcomp(p, (int)p_magic);
  962.     vim_free(pat);
  963.     if (prog == NULL)
  964.         return -1;
  965.  
  966.     for (buf = firstbuf; buf != NULL; buf = buf->b_next)
  967.     {
  968.         match = buflist_match(prog, buf);
  969.         if (match != NULL)
  970.         {
  971.         if (fnum >= 0)        /* already found a match */
  972.         {
  973.             fnum = -2;
  974.             break;
  975.         }
  976.         fnum = buf->b_fnum;    /* remember first match */
  977.         }
  978.     }
  979.     vim_free(prog);
  980.     if (fnum >= 0)            /* found one match */
  981.         break;
  982.     }
  983.  
  984.     if (fnum == -2)
  985.     EMSG2("More than one match for %s", pattern);
  986.     else if (fnum < 1)
  987.     EMSG2("No matching buffer for %s", pattern);
  988.     return fnum;
  989. }
  990.  
  991. /*
  992.  * Find all buffer names that match.
  993.  * For command line expansion of ":buf" and ":sbuf".
  994.  * Return OK if matches found, FAIL otherwise.
  995.  */
  996.     int
  997. ExpandBufnames(pat, num_file, file, options)
  998.     char_u    *pat;
  999.     int        *num_file;
  1000.     char_u    ***file;
  1001.     int        options;
  1002. {
  1003.     int        count = 0;
  1004.     BUF        *buf;
  1005.     int        round;
  1006.     char_u    *p;
  1007.     int        attempt;
  1008.     vim_regexp    *prog;
  1009.  
  1010.     *num_file = 0;            /* return values in case of FAIL */
  1011.     *file = NULL;
  1012.  
  1013.     /*
  1014.      * attempt == 1: try match with    '^', match at start
  1015.      * attempt == 2: try match without '^', match anywhere
  1016.      */
  1017.     for (attempt = 1; attempt <= 2; ++attempt)
  1018.     {
  1019.     if (attempt == 2)
  1020.     {
  1021.         if (*pat != '^')        /* there's no '^', no need to try again */
  1022.         break;
  1023.         ++pat;            /* skip the '^' */
  1024.     }
  1025.     prog = vim_regcomp(pat, (int)p_magic);
  1026.     if (prog == NULL)
  1027.         return FAIL;
  1028.  
  1029.     /*
  1030.      * round == 1: Count the matches.
  1031.      * round == 2: Build the array to keep the matches.
  1032.      */
  1033.     for (round = 1; round <= 2; ++round)
  1034.     {
  1035.         count = 0;
  1036.         for (buf = firstbuf; buf != NULL; buf = buf->b_next)
  1037.         {
  1038.         p = buflist_match(prog, buf);
  1039.         if (p != NULL)
  1040.         {
  1041.             if (round == 1)
  1042.             ++count;
  1043.             else
  1044.             {
  1045.             if (options & WILD_HOME_REPLACE)
  1046.                 p = home_replace_save(buf, p);
  1047.             else
  1048.                 p = vim_strsave(p);
  1049.             (*file)[count++] = p;
  1050.             }
  1051.         }
  1052.         }
  1053.         if (count == 0)    /* no match found, break here */
  1054.         break;
  1055.         if (round == 1)
  1056.         {
  1057.         *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
  1058.         if (*file == NULL)
  1059.         {
  1060.             vim_free(prog);
  1061.             return FAIL;
  1062.         }
  1063.         }
  1064.     }
  1065.     vim_free(prog);
  1066.     if (count)        /* match(es) found, break here */
  1067.         break;
  1068.     }
  1069.  
  1070.     *num_file = count;
  1071.     return (count == 0 ? FAIL : OK);
  1072. }
  1073.  
  1074. /*
  1075.  * Check for a match on the file name for buffer "buf" with regex prog "prog".
  1076.  */
  1077.     static char_u *
  1078. buflist_match(prog, buf)
  1079.     vim_regexp    *prog;
  1080.     BUF        *buf;
  1081. {
  1082.     char_u  *match;
  1083. #ifdef CASE_INSENSITIVE_FILENAME
  1084.     int        save_reg_ic = reg_ic;
  1085.  
  1086.     reg_ic = TRUE;        /* Always ignore case */
  1087. #endif
  1088.  
  1089.     /* First try the short file name, then the long file name. */
  1090.     match = buflist_match_try(prog, buf->b_sfname);
  1091.     if (match == NULL)
  1092.     match = buflist_match_try(prog, buf->b_ffname);
  1093.  
  1094. #ifdef CASE_INSENSITIVE_FILENAME
  1095.     reg_ic = save_reg_ic;
  1096. #endif
  1097.  
  1098.     return match;
  1099. }
  1100.  
  1101.     static char_u *
  1102. buflist_match_try(prog, name)
  1103.     vim_regexp    *prog;
  1104.     char_u    *name;
  1105. {
  1106.     char_u  *match = NULL;
  1107.     char_u  *p;
  1108.  
  1109.     if (name != NULL)
  1110.     {
  1111.     if (vim_regexec(prog, name, TRUE) != 0)
  1112.         match = name;
  1113.     else
  1114.     {
  1115.         /* Replace $(HOME) with '~' and try matching again. */
  1116.         p = home_replace_save(NULL, name);
  1117.         if (p != NULL && vim_regexec(prog, p, TRUE) != 0)
  1118.         match = name;
  1119.         vim_free(p);
  1120.     }
  1121.     }
  1122.     return match;
  1123. }
  1124.  
  1125. /*
  1126.  * find file in buffer name list by number
  1127.  */
  1128.     BUF    *
  1129. buflist_findnr(nr)
  1130.     int        nr;
  1131. {
  1132.     BUF        *buf;
  1133.  
  1134.     if (nr == 0)
  1135.     nr = curwin->w_alt_fnum;
  1136.     for (buf = firstbuf; buf != NULL; buf = buf->b_next)
  1137.     if (buf->b_fnum == nr)
  1138.         return (buf);
  1139.     return NULL;
  1140. }
  1141.  
  1142. /*
  1143.  * Get name of file 'n' in the buffer list.
  1144.  * home_replace() is used to shorten the file name (used for marks).
  1145.  * Returns a pointer to allocated memory, of NULL when failed.
  1146.  */
  1147.     char_u *
  1148. buflist_nr2name(n, fullname, helptail)
  1149.     int n;
  1150.     int fullname;
  1151.     int helptail;        /* for help buffers return tail only */
  1152. {
  1153.     BUF        *buf;
  1154.  
  1155.     buf = buflist_findnr(n);
  1156.     if (buf == NULL)
  1157.     return NULL;
  1158.     return home_replace_save(helptail ? buf : NULL,
  1159.                      fullname ? buf->b_ffname : buf->b_fname);
  1160. }
  1161.  
  1162. /*
  1163.  * set the lnum for the buffer 'buf' and the current window
  1164.  */
  1165.     static void
  1166. buflist_setlnum(buf, lnum, col)
  1167.     BUF        *buf;
  1168.     linenr_t    lnum;
  1169.     colnr_t    col;
  1170. {
  1171.     WINLNUM    *wlp;
  1172.  
  1173.     for (wlp = buf->b_winlnum; wlp != NULL; wlp = wlp->wl_next)
  1174.     if (wlp->wl_win == curwin)
  1175.         break;
  1176.     if (wlp == NULL)        /* make new entry */
  1177.     {
  1178.     wlp = (WINLNUM *)alloc((unsigned)sizeof(WINLNUM));
  1179.     if (wlp == NULL)
  1180.         return;
  1181.     wlp->wl_win = curwin;
  1182.     }
  1183.     else            /* remove entry from list */
  1184.     {
  1185.     if (wlp->wl_prev)
  1186.         wlp->wl_prev->wl_next = wlp->wl_next;
  1187.     else
  1188.         buf->b_winlnum = wlp->wl_next;
  1189.     if (wlp->wl_next)
  1190.         wlp->wl_next->wl_prev = wlp->wl_prev;
  1191.     }
  1192.     wlp->wl_lnum = lnum;
  1193.     wlp->wl_col = col;
  1194.  
  1195.     /* insert entry in front of the list */
  1196.     wlp->wl_next = buf->b_winlnum;
  1197.     buf->b_winlnum = wlp;
  1198.     wlp->wl_prev = NULL;
  1199.     if (wlp->wl_next)
  1200.     wlp->wl_next->wl_prev = wlp;
  1201.  
  1202.     return;
  1203. }
  1204.  
  1205. /*
  1206.  * find the wlp for the buffer 'buf' for the current window
  1207.  */
  1208.     static WINLNUM *
  1209. buflist_findwlp(buf)
  1210.     BUF        *buf;
  1211. {
  1212.     WINLNUM    *wlp;
  1213.  
  1214.     for (wlp = buf->b_winlnum; wlp != NULL; wlp = wlp->wl_next)
  1215.     if (wlp->wl_win == curwin)
  1216.         break;
  1217.  
  1218.     if (wlp == NULL)    /* if no lnum for curwin, use the first in the list */
  1219.     wlp = buf->b_winlnum;
  1220.  
  1221.     return wlp;
  1222. }
  1223.  
  1224. /*
  1225.  * find the lnum for the buffer 'buf' for the current window
  1226.  */
  1227.     linenr_t
  1228. buflist_findlnum(buf)
  1229.     BUF        *buf;
  1230. {
  1231.     WINLNUM    *wlp;
  1232.  
  1233.     wlp = buflist_findwlp(buf);
  1234.  
  1235.     if (wlp != NULL)
  1236.     return wlp->wl_lnum;
  1237.     else
  1238.     return (linenr_t)1;
  1239. }
  1240.  
  1241. /*
  1242.  * list all know file names (for :files and :buffers command)
  1243.  */
  1244.     void
  1245. buflist_list()
  1246. {
  1247.     BUF        *buf;
  1248.     int        len;
  1249.  
  1250.     for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
  1251.     {
  1252.     msg_putchar('\n');
  1253.     if (buf->b_fname == NULL)
  1254.         STRCPY(NameBuff, "No File");
  1255.     else
  1256.         home_replace(buf, buf->b_fname, NameBuff, MAXPATHL);
  1257.  
  1258.     sprintf((char *)IObuff, "%3d %c%c%c \"",
  1259.         buf->b_fnum,
  1260.         buf == curbuf ? '%' :
  1261.             (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
  1262.         buf->b_ml.ml_mfp == NULL ? '-' :
  1263.             (buf->b_nwindows == 0 ? 'h' : ' '),
  1264.         buf_changed(buf) ? '+' : ' ');
  1265.  
  1266.     len = STRLEN(IObuff);
  1267.     STRNCPY(IObuff + len, NameBuff, IOSIZE - 20 - len);
  1268.  
  1269.     len = STRLEN(IObuff);
  1270.     IObuff[len++] = '"';
  1271.     /*
  1272.      * try to put the "line" strings in column 40
  1273.      */
  1274.     do
  1275.     {
  1276.         IObuff[len++] = ' ';
  1277.     } while (len < 40 && len < IOSIZE - 18);
  1278.     sprintf((char *)IObuff + len, "line %ld",
  1279.         buf == curbuf ? curwin->w_cursor.lnum :
  1280.                 (long)buflist_findlnum(buf));
  1281.     msg_outtrans(IObuff);
  1282.     out_flush();        /* output one line at a time */
  1283.     ui_breakcheck();
  1284.     }
  1285. }
  1286.  
  1287. /*
  1288.  * Get file name and line number for file 'fnum'.
  1289.  * Used by DoOneCmd() for translating '%' and '#'.
  1290.  * Used by insert_reg() and cmdline_paste() for '#' register.
  1291.  * Return FAIL if not found, OK for success.
  1292.  */
  1293.     int
  1294. buflist_name_nr(fnum, fname, lnum)
  1295.     int        fnum;
  1296.     char_u    **fname;
  1297.     linenr_t    *lnum;
  1298. {
  1299.     BUF        *buf;
  1300.  
  1301.     buf = buflist_findnr(fnum);
  1302.     if (buf == NULL || buf->b_fname == NULL)
  1303.     return FAIL;
  1304.  
  1305.     *fname = buf->b_fname;
  1306.     *lnum = buflist_findlnum(buf);
  1307.  
  1308.     return OK;
  1309. }
  1310.  
  1311. /*
  1312.  * Set the current file name to 'ffname', short file name to 'sfname'.
  1313.  * The file name with the full path is also remembered, for when :cd is used.
  1314.  * Returns FAIL for failure (file name already in use by other buffer)
  1315.  *    OK otherwise.
  1316.  */
  1317.     int
  1318. setfname(ffname, sfname, message)
  1319.     char_u *ffname, *sfname;
  1320.     int        message;
  1321. {
  1322.     BUF        *buf;
  1323.  
  1324.     if (ffname == NULL || *ffname == NUL)
  1325.     {
  1326.     vim_free(curbuf->b_ffname);
  1327.     vim_free(curbuf->b_sfname);
  1328.     curbuf->b_ffname = NULL;
  1329.     curbuf->b_sfname = NULL;
  1330.     }
  1331.     else
  1332.     {
  1333.     fname_expand(&ffname, &sfname);        /* will allocate ffname */
  1334.     if (ffname == NULL)            /* out of memory */
  1335.         return FAIL;
  1336.  
  1337. #ifdef USE_FNAME_CASE
  1338. # ifdef USE_LONG_FNAME
  1339.     if (USE_LONG_FNAME)
  1340. # endif
  1341.         fname_case(sfname);        /* set correct case for short file name */
  1342. #endif
  1343.     /*
  1344.      * if the file name is already used in another buffer:
  1345.      * - if the buffer is loaded, fail
  1346.      * - if the buffer is not loaded, delete it from the list
  1347.      */
  1348.     buf = buflist_findname(ffname);
  1349.     if (buf != NULL && buf != curbuf)
  1350.     {
  1351.         if (buf->b_ml.ml_mfp != NULL)    /* it's loaded, fail */
  1352.         {
  1353.         if (message)
  1354.             EMSG("Buffer with this name already exists");
  1355.         vim_free(ffname);
  1356.         return FAIL;
  1357.         }
  1358.         close_buffer(NULL, buf, TRUE, TRUE);    /* delete from the list */
  1359.     }
  1360.     sfname = vim_strsave(sfname);
  1361.     if (ffname == NULL || sfname == NULL)
  1362.     {
  1363.         vim_free(sfname);
  1364.         vim_free(ffname);
  1365.         return FAIL;
  1366.     }
  1367.     vim_free(curbuf->b_ffname);
  1368.     vim_free(curbuf->b_sfname);
  1369.     curbuf->b_ffname = ffname;
  1370.     curbuf->b_sfname = sfname;
  1371.     }
  1372.     curbuf->b_fname = curbuf->b_sfname;
  1373.  
  1374. #ifndef SHORT_FNAME
  1375.     curbuf->b_shortname = FALSE;
  1376. #endif
  1377.     /*
  1378.      * If the file name changed, also change the name of the swapfile
  1379.      */
  1380.     if (curbuf->b_ml.ml_mfp != NULL)
  1381.     ml_setname();
  1382.  
  1383.     check_arg_idx();        /* check file name for arg list */
  1384.     maketitle();        /* set window title */
  1385.     status_redraw_all();    /* status lines need to be redrawn */
  1386.     fmarks_check_names(curbuf);    /* check named file marks */
  1387.     ml_timestamp(curbuf);    /* reset timestamp */
  1388.     return OK;
  1389. }
  1390.  
  1391. /*
  1392.  * set alternate file name for current window
  1393.  *
  1394.  * Used by do_one_cmd(), do_write() and do_ecmd().
  1395.  */
  1396.     void
  1397. setaltfname(ffname, sfname, lnum)
  1398.     char_u    *ffname;
  1399.     char_u    *sfname;
  1400.     linenr_t    lnum;
  1401. {
  1402.     BUF        *buf;
  1403.  
  1404.     buf = buflist_new(ffname, sfname, lnum, FALSE);
  1405.     if (buf != NULL)
  1406.     curwin->w_alt_fnum = buf->b_fnum;
  1407. }
  1408.  
  1409. /*
  1410.  * Get alternate file name for current window.
  1411.  * Return NULL if there isn't any, and give error message.
  1412.  */
  1413.     char_u  *
  1414. getaltfname()
  1415. {
  1416.     char_u    *fname;
  1417.     linenr_t    dummy;
  1418.  
  1419.     if (buflist_name_nr(0, &fname, &dummy) == FAIL)
  1420.     {
  1421.     emsg(e_noalt);
  1422.     return NULL;
  1423.     }
  1424.     return fname;
  1425. }
  1426.  
  1427. /*
  1428.  * add a file name to the buflist and return its number
  1429.  *
  1430.  * used by qf_init(), main() and doarglist()
  1431.  */
  1432.     int
  1433. buflist_add(fname)
  1434.     char_u    *fname;
  1435. {
  1436.     BUF        *buf;
  1437.  
  1438.     buf = buflist_new(fname, NULL, (linenr_t)0, FALSE);
  1439.     if (buf != NULL)
  1440.     return buf->b_fnum;
  1441.     return 0;
  1442. }
  1443.  
  1444. /*
  1445.  * set alternate lnum for current window
  1446.  */
  1447.     void
  1448. buflist_altlnum()
  1449. {
  1450.     buflist_setlnum(curbuf, curwin->w_cursor.lnum, curwin->w_cursor.col);
  1451. }
  1452.  
  1453. /*
  1454.  * Return TRUE if 'fname' is not the same file as current file.
  1455.  * Fname must have a full path (expanded by mch_FullName).
  1456.  */
  1457.     int
  1458. otherfile(fname)
  1459.     char_u  *fname;
  1460. {                    /* no name is different */
  1461.     if (fname == NULL || *fname == NUL || curbuf->b_ffname == NULL)
  1462.     return TRUE;
  1463.     if (fnamecmp(fname, curbuf->b_ffname) == 0)
  1464.     return FALSE;
  1465. #ifdef UNIX
  1466.     {
  1467.     struct stat    st1, st2;
  1468.  
  1469.     /* Use stat() to check if the files are the same, even when the names
  1470.      * are different (possible with links) */
  1471.     if (       stat((char *)fname, &st1) >= 0
  1472.         && stat((char *)curbuf->b_ffname, &st2) >= 0
  1473.         && st1.st_dev == st2.st_dev
  1474.         && st1.st_ino == st2.st_ino)
  1475.         return FALSE;
  1476.     }
  1477. #endif
  1478.     return TRUE;
  1479. }
  1480.  
  1481.     void
  1482. fileinfo(fullname, shorthelp, dont_truncate)
  1483.     int fullname;
  1484.     int shorthelp;
  1485.     int    dont_truncate;
  1486. {
  1487.     char_u    *name;
  1488.     int        n;
  1489.     char_u    *p;
  1490.     char_u    *buffer;
  1491.  
  1492.     buffer = alloc(IOSIZE);
  1493.     if (buffer == NULL)
  1494.     return;
  1495.  
  1496.     if (fullname > 1)        /* 2 CTRL-G: include buffer number */
  1497.     {
  1498.     sprintf((char *)buffer, "buf %d: ", curbuf->b_fnum);
  1499.     p = buffer + STRLEN(buffer);
  1500.     }
  1501.     else
  1502.     p = buffer;
  1503.  
  1504.     *p++ = '"';
  1505.     if (curbuf->b_ffname == NULL)
  1506.     STRCPY(p, "No File");
  1507.     else
  1508.     {
  1509.     if (!fullname && curbuf->b_fname != NULL)
  1510.         name = curbuf->b_fname;
  1511.     else
  1512.         name = curbuf->b_ffname;
  1513.     home_replace(shorthelp ? curbuf : NULL, name, p,
  1514.                         (int)(IOSIZE - (p - buffer)));
  1515.     }
  1516.  
  1517.     sprintf((char *)buffer + STRLEN(buffer),
  1518.         "\"%s%s%s%s",
  1519.         curbuf_changed() ? (shortmess(SHM_MOD) ?
  1520.                         " [+]" : " [Modified]") : " ",
  1521.         curbuf->b_notedited ? "[Not edited]" : "",
  1522.         curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]" : "[readonly]") : "",
  1523.         (curbuf_changed() || curbuf->b_notedited || curbuf->b_p_ro) ?
  1524.                                     " " : "");
  1525.     n = (int)(((long)curwin->w_cursor.lnum * 100L) /
  1526.                         (long)curbuf->b_ml.ml_line_count);
  1527.     if (curbuf->b_ml.ml_flags & ML_EMPTY)
  1528.     {
  1529.     STRCPY(buffer + STRLEN(buffer), no_lines_msg);
  1530.     }
  1531.     else if (p_ru)
  1532.     {
  1533.     /* Current line and column are already on the screen -- webb */
  1534.     sprintf((char *)buffer + STRLEN(buffer),
  1535.         "%ld line%s --%d%%--",
  1536.         (long)curbuf->b_ml.ml_line_count,
  1537.         plural((long)curbuf->b_ml.ml_line_count),
  1538.         n);
  1539.     }
  1540.     else
  1541.     {
  1542.     sprintf((char *)buffer + STRLEN(buffer),
  1543.         "line %ld of %ld --%d%%-- col ",
  1544.         (long)curwin->w_cursor.lnum,
  1545.         (long)curbuf->b_ml.ml_line_count,
  1546.         n);
  1547.     validate_virtcol();
  1548.     col_print(buffer + STRLEN(buffer),
  1549.            (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
  1550.     }
  1551.  
  1552.     (void)append_arg_number(buffer, !shortmess(SHM_FILE), IOSIZE);
  1553.  
  1554.     if (dont_truncate)
  1555.     msg(buffer);
  1556.     else
  1557.     msg_trunc(buffer);
  1558.  
  1559.     vim_free(buffer);
  1560. }
  1561.  
  1562. /*
  1563.  * Give some info about the position of the cursor (for "g CTRL-G").
  1564.  */
  1565.     void
  1566. cursor_pos_info()
  1567. {
  1568.     char_u    *p;
  1569.     char_u    buf1[20];
  1570.     char_u    buf2[20];
  1571.     linenr_t    lnum;
  1572.     long    char_count = 0;
  1573.     long    char_count_cursor = 0;
  1574.     int        eol_size;
  1575.  
  1576.     /*
  1577.      * Compute the length of the file in characters.
  1578.      */
  1579.     if (curbuf->b_ml.ml_flags & ML_EMPTY)
  1580.     {
  1581.     MSG(no_lines_msg);
  1582.     }
  1583.     else
  1584.     {
  1585.     if (get_fileformat(curbuf) == EOL_DOS)
  1586.         eol_size = 2;
  1587.     else
  1588.         eol_size = 1;
  1589.     for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
  1590.     {
  1591.         if (lnum == curwin->w_cursor.lnum)
  1592.         char_count_cursor = char_count + curwin->w_cursor.col + 1;
  1593.         char_count += STRLEN(ml_get(lnum)) + eol_size;
  1594.     }
  1595.     if (!curbuf->b_p_eol && curbuf->b_p_bin)
  1596.         char_count -= eol_size;
  1597.  
  1598.     p = ml_get_curline();
  1599.     validate_virtcol();
  1600.     col_print(buf1, (int)curwin->w_cursor.col + 1,
  1601.                           (int)curwin->w_virtcol + 1);
  1602.     col_print(buf2, (int)STRLEN(p), linetabsize(p));
  1603.  
  1604.     sprintf((char *)IObuff, "Col %s of %s; Line %ld of %ld; Char %ld of %ld",
  1605.         (char *)buf1, (char *)buf2,
  1606.         (long)curwin->w_cursor.lnum, (long)curbuf->b_ml.ml_line_count,
  1607.         char_count_cursor, char_count);
  1608.     msg(IObuff);
  1609.     }
  1610. }
  1611.  
  1612.     void
  1613. col_print(buf, col, vcol)
  1614.     char_u  *buf;
  1615.     int        col;
  1616.     int        vcol;
  1617. {
  1618.     if (col == vcol)
  1619.     sprintf((char *)buf, "%d", col);
  1620.     else
  1621.     sprintf((char *)buf, "%d-%d", col, vcol);
  1622. }
  1623.  
  1624. /*
  1625.  * put file name in title bar of window and in icon title
  1626.  */
  1627.  
  1628. static char_u *lasttitle = NULL;
  1629. static char_u *lasticon = NULL;
  1630.  
  1631.     void
  1632. maketitle()
  1633. {
  1634.     char_u    *t_name;
  1635.     char_u    *i_name;
  1636.     int        maxlen;
  1637.     int        len;
  1638.     char_u    *buf = NULL;
  1639.  
  1640.     if (curbuf->b_ffname == NULL)
  1641.     {
  1642.     t_name = (char_u *)"VIM -";
  1643.     i_name = (char_u *)"No File";
  1644.     }
  1645.     else
  1646.     {
  1647.     buf = alloc(IOSIZE);
  1648.     if (buf == NULL)
  1649.         return;
  1650.     STRCPY(buf, "VIM - ");
  1651.     home_replace(curbuf, curbuf->b_ffname, buf + 6, IOSIZE - 6);
  1652.     append_arg_number(buf, FALSE, IOSIZE);
  1653.     if (p_titlelen > 0)
  1654.     {
  1655.         maxlen = p_titlelen * Columns / 100;
  1656.         if (maxlen < 10)
  1657.         maxlen = 10;
  1658.         len = STRLEN(buf);
  1659.         if (len > maxlen)
  1660.         {
  1661.         vim_memmove(buf + 6, buf + 6 + len - maxlen,
  1662.                               (size_t)maxlen - 5);
  1663.         buf[5] = '<';
  1664.         }
  1665.     }
  1666.     t_name = buf;
  1667.     i_name = gettail(curbuf->b_ffname); /* use file name only for icon */
  1668.     }
  1669.  
  1670.     if (*p_titlestring)
  1671.     t_name = p_titlestring;
  1672.     if (*p_iconstring)
  1673.     i_name = p_iconstring;
  1674.  
  1675.     vim_free(lasttitle);
  1676.     if (p_title && (lasttitle = alloc((unsigned)(vim_strsize(t_name) + 1)))
  1677.                                       != NULL)
  1678.     {
  1679.     *lasttitle = NUL;
  1680.     while (*t_name)
  1681.         STRCAT(lasttitle, transchar(*t_name++));
  1682.     }
  1683.     else
  1684.     lasttitle = NULL;
  1685.     vim_free(buf);
  1686.  
  1687.     vim_free(lasticon);
  1688.     if (p_icon && (lasticon = alloc((unsigned)(vim_strsize(i_name) + 1)))
  1689.                                       != NULL)
  1690.     {
  1691.     *lasticon = NUL;
  1692.     while (*i_name)
  1693.         STRCAT(lasticon, transchar(*i_name++));
  1694.     }
  1695.     else
  1696.     lasticon = NULL;
  1697.  
  1698.     resettitle();
  1699. }
  1700.  
  1701. /*
  1702.  * Append (file 2 of 8) to 'buf', if editing more than one file.
  1703.  * Return TRUE if it was appended.
  1704.  */
  1705.     static int
  1706. append_arg_number(buf, add_file, maxlen)
  1707.     char_u  *buf;
  1708.     int        add_file;        /* Add "file" before the arg number */
  1709.     int        maxlen;        /* maximum nr of chars in buf */
  1710. {
  1711.     char_u    *p;
  1712.  
  1713.     if (arg_file_count <= 1)        /* nothing to do */
  1714.     return FALSE;
  1715.  
  1716.     p = buf + STRLEN(buf);    /* go to the end of the buffer */
  1717.     if (p - buf + 35 >= maxlen)    /* getting too long */
  1718.     return FALSE;
  1719.     *p++ = ' ';
  1720.     *p++ = '(';
  1721.     if (add_file)
  1722.     {
  1723.     STRCPY(p, "file ");
  1724.     p += 5;
  1725.     }
  1726.     sprintf((char *)p, curwin->w_arg_idx_invalid ? "(%d) of %d)" :
  1727.               "%d of %d)", curwin->w_arg_idx + 1, arg_file_count);
  1728.     return TRUE;
  1729. }
  1730.  
  1731. /*
  1732.  * Put current window title back (used after calling a shell)
  1733.  */
  1734.     void
  1735. resettitle()
  1736. {
  1737.     mch_settitle(lasttitle, lasticon);
  1738. }
  1739.  
  1740. /*
  1741.  * If fname is not a full path, make it a full path.
  1742.  * Returns pointer to allocated memory (NULL for failure).
  1743.  */
  1744.     char_u  *
  1745. fix_fname(fname)
  1746.     char_u  *fname;
  1747. {
  1748.     /*
  1749.      * Force expanding the path always for Unix, because symbolic links may
  1750.      * mess up the full path name, even though it starts with a '/'.
  1751.      */
  1752. #ifdef UNIX
  1753.     return FullName_save(fname, TRUE);
  1754. #else
  1755.     if (!mch_isFullName(fname))
  1756.     return FullName_save(fname, FALSE);
  1757.  
  1758.     fname = vim_strsave(fname);
  1759.  
  1760. #ifdef USE_FNAME_CASE
  1761. # ifdef USE_LONG_FNAME
  1762.     if (USE_LONG_FNAME)
  1763. # endif
  1764.     {
  1765.     if (fname != NULL)
  1766.         fname_case(fname);        /* set correct case for file name */
  1767.     }
  1768. #endif
  1769.  
  1770.     return fname;
  1771. #endif
  1772. }
  1773.  
  1774. /*
  1775.  * make ffname a full file name, set sfname to ffname if not NULL
  1776.  * ffname becomes a pointer to allocated memory (or NULL).
  1777.  */
  1778.     void
  1779. fname_expand(ffname, sfname)
  1780.     char_u    **ffname;
  1781.     char_u    **sfname;
  1782. {
  1783.     if (*ffname == NULL)    /* if no file name given, nothing to do */
  1784.     return;
  1785.     if (*sfname == NULL)    /* if no short file name given, use ffname */
  1786.     *sfname = *ffname;
  1787.     *ffname = fix_fname(*ffname);   /* expand to full path */
  1788. }
  1789.  
  1790. /*
  1791.  * do_arg_all(): Open up to 'count' windows, one for each argument.
  1792.  */
  1793.     void
  1794. do_arg_all(count, forceit)
  1795.     int    count;
  1796.     int    forceit;        /* hide buffers in current windows */
  1797. {
  1798.     int        i;
  1799.     WIN        *wp, *wpnext;
  1800.     char_u    *opened;    /* array of flags for which args are open */
  1801.     int        opened_len;    /* lenght of opened[] */
  1802.     int        use_firstwin = FALSE;    /* use first window for arglist */
  1803.     int        split_ret = OK;
  1804.     int        p_sb_save;
  1805.     int        p_ea_save;
  1806.  
  1807.     if (arg_file_count <= 0)
  1808.     {
  1809.     /* Don't give an error message.  We don't want it when the ":all"
  1810.      * command is in the .vimrc. */
  1811.     return;
  1812.     }
  1813.     setpcmark();
  1814.  
  1815.     opened_len = arg_file_count;
  1816.     opened = alloc_clear((unsigned)opened_len);
  1817.     if (opened == NULL)
  1818.     return;
  1819.  
  1820.     /*
  1821.      * Try closing all windows that are not in the argument list.
  1822.      * When 'hidden' or "forceit" set the buffer becomes hidden.
  1823.      * Windows that have a changed buffer and can't be hidden won't be closed.
  1824.      */
  1825.     for (wp = firstwin; wp != NULL; wp = wpnext)
  1826.     {
  1827.     wpnext = wp->w_next;
  1828.     if (wp->w_buffer->b_ffname == NULL || wp->w_buffer->b_nwindows > 1)
  1829.         i = arg_file_count;
  1830.     else
  1831.     {
  1832.         /* check if the buffer in this window is in the arglist */
  1833.         for (i = 0; i < arg_file_count; ++i)
  1834.         {
  1835.         if (fullpathcmp(arg_files[i],
  1836.                      wp->w_buffer->b_ffname, TRUE) & FPC_SAME)
  1837.         {
  1838.             if (i < opened_len)
  1839.             opened[i] = TRUE;
  1840.             break;
  1841.         }
  1842.         }
  1843.     }
  1844.     wp->w_arg_idx = i;
  1845.  
  1846.     if (i == arg_file_count)        /* close this window */
  1847.     {
  1848.         if (p_hid || forceit || wp->w_buffer->b_nwindows > 1
  1849.                         || !buf_changed(wp->w_buffer))
  1850.         {
  1851.         /* If the buffer was changed, and we would like to hide it,
  1852.          * try autowriting. */
  1853.         if (!p_hid && wp->w_buffer->b_nwindows <= 1
  1854.                          && buf_changed(wp->w_buffer))
  1855.         {
  1856.             autowrite(wp->w_buffer, FALSE);
  1857. #ifdef AUTOCMD
  1858.             /* check if autocommands removed the window */
  1859.             if (!win_valid(wp))
  1860.             {
  1861.             wpnext = firstwin;    /* start all over... */
  1862.             continue;
  1863.             }
  1864. #endif
  1865.         }
  1866.         if (firstwin == lastwin)    /* can't close last window */
  1867.             use_firstwin = TRUE;
  1868.         else
  1869.         {
  1870.             close_window(wp, !p_hid && !buf_changed(wp->w_buffer));
  1871. #ifdef AUTOCMD
  1872.             /* check if autocommands removed the next window */
  1873.             if (!win_valid(wpnext))
  1874.             wpnext = firstwin;    /* start all over... */
  1875. #endif
  1876.         }
  1877.         }
  1878.     }
  1879.     }
  1880.  
  1881.     /*
  1882.      * Open a window for files in the argument list that don't have one.
  1883.      * arg_file_count may change while doing this, because of autocommands.
  1884.      */
  1885.     if (count > arg_file_count || count <= 0)
  1886.     count = arg_file_count;
  1887.  
  1888. #ifdef AUTOCMD
  1889.     /* Don't execute Win/Buf Enter/Leave autocommands here. */
  1890.     ++autocmd_no_enter;
  1891.     ++autocmd_no_leave;
  1892. #endif
  1893.     win_enter(lastwin, FALSE);
  1894.     for (i = 0; i < count && i < arg_file_count && !got_int; ++i)
  1895.     {
  1896.     if (i == arg_file_count - 1)
  1897.         arg_had_last = TRUE;
  1898.     if (i < opened_len && opened[i])
  1899.     {
  1900.         /* Move the already present window to below the current window */
  1901.         if (curwin->w_arg_idx != i)
  1902.         {
  1903.         for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
  1904.         {
  1905.             if (wpnext->w_arg_idx == i)
  1906.             {
  1907.             win_move_after(wpnext, curwin);
  1908.             break;
  1909.             }
  1910.         }
  1911.         }
  1912.     }
  1913.     else if (split_ret == OK)
  1914.     {
  1915.         if (!use_firstwin)        /* split current window */
  1916.         {
  1917.         p_sb_save = p_sb;
  1918.         p_ea_save = p_ea;
  1919.         p_sb = TRUE;        /* put windows in order of arglist */
  1920.         p_ea = TRUE;        /* use space from all windows */
  1921.         split_ret = win_split(0, FALSE, TRUE);
  1922.         p_sb = p_sb_save;
  1923.         p_ea = p_ea_save;
  1924.         if (split_ret == FAIL)
  1925.             continue;
  1926.         }
  1927. #ifdef AUTOCMD
  1928.         else    /* first window: do autocmd for leaving this buffer */
  1929.         --autocmd_no_leave;
  1930. #endif
  1931.  
  1932.         curwin->w_arg_idx = i;
  1933.         /* edit file i */
  1934.         (void)do_ecmd(0, arg_files[i], NULL, NULL, (linenr_t)1,
  1935.            ((p_hid || buf_changed(curwin->w_buffer)) ? ECMD_HIDE : 0)
  1936.                                    + ECMD_OLDBUF);
  1937. #ifdef AUTOCMD
  1938.         if (use_firstwin)
  1939.         ++autocmd_no_leave;
  1940. #endif
  1941.         use_firstwin = FALSE;
  1942.     }
  1943.     ui_breakcheck();
  1944.     }
  1945. #ifdef AUTOCMD
  1946.     --autocmd_no_enter;
  1947. #endif
  1948.     win_enter(firstwin, FALSE);            /* back to first window */
  1949. #ifdef AUTOCMD
  1950.     --autocmd_no_leave;
  1951. #endif
  1952. }
  1953.  
  1954. /*
  1955.  * do_buffer_all: Open a window for each buffer.
  1956.  *
  1957.  * 'count' is the maximum number of windows to open.
  1958.  * When 'all' is TRUE, also load inactive buffers.
  1959.  */
  1960.     void
  1961. do_buffer_all(count, all)
  1962.     int        count;
  1963.     int        all;
  1964. {
  1965.     BUF        *buf;
  1966.     WIN        *wp, *wpnext;
  1967.     int        split_ret = OK;
  1968.     int        p_sb_save;
  1969.     int        p_ea_save;
  1970.     int        open_wins = 0;
  1971.  
  1972.     setpcmark();
  1973.  
  1974.     /*
  1975.      * Close superfluous windows (two windows for the same buffer).
  1976.      */
  1977.     for (wp = firstwin; wp != NULL; wp = wpnext)
  1978.     {
  1979.     wpnext = wp->w_next;
  1980.     if (wp->w_buffer->b_nwindows > 1)
  1981.     {
  1982.         close_window(wp, FALSE);
  1983. #ifdef AUTOCMD
  1984.         wpnext = firstwin;        /* just in case an autocommand does
  1985.                        something strange with windows */
  1986.         open_wins = 0;
  1987. #endif
  1988.     }
  1989.     else
  1990.         ++open_wins;
  1991.     }
  1992.  
  1993.     /*
  1994.      * Go through the buffer list.  When a buffer doesn't have a window yet,
  1995.      * open one.  Otherwise move the window to the right position.
  1996.      * Watch out for autocommands that delete buffers or windows!
  1997.      */
  1998. #ifdef AUTOCMD
  1999.     /* Don't execute Win/Buf Enter/Leave autocommands here. */
  2000.     ++autocmd_no_enter;
  2001. #endif
  2002.     win_enter(lastwin, FALSE);
  2003. #ifdef AUTOCMD
  2004.     ++autocmd_no_leave;
  2005. #endif
  2006.     for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
  2007.     {
  2008.     /* Check if this buffer needs a window */
  2009.     if (!all && buf->b_ml.ml_mfp == NULL)
  2010.         continue;
  2011.  
  2012.     /* Check if this buffer already has a window */
  2013.     for (wp = firstwin; wp != NULL; wp = wp->w_next)
  2014.         if (wp->w_buffer == buf)
  2015.         break;
  2016.     /* If the buffer already has a window, move it */
  2017.     if (wp != NULL)
  2018.         win_move_after(wp, curwin);
  2019.     else if (split_ret == OK)
  2020.     {
  2021.         /* Split the window and put the buffer in it */
  2022.         p_sb_save = p_sb;
  2023.         p_ea_save = p_ea;
  2024.         p_sb = TRUE;        /* put windows in order of arglist */
  2025.         p_ea = TRUE;        /* use space from all windows */
  2026.         split_ret = win_split(0, FALSE, TRUE);
  2027.         ++open_wins;
  2028.         p_sb = p_sb_save;
  2029.         p_ea = p_ea_save;
  2030.         if (split_ret == FAIL)
  2031.         continue;
  2032.         (void)do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD,
  2033.                              (int)buf->b_fnum, 0);
  2034. #ifdef AUTOCMD
  2035.         if (!buf_valid(buf))    /* autocommands deleted the buffer!!! */
  2036.         break;
  2037. #endif
  2038.     }
  2039.  
  2040.     ui_breakcheck();
  2041.     if (got_int)
  2042.     {
  2043.         (void)vgetc();    /* only break the file loading, not the rest */
  2044.         break;
  2045.     }
  2046.     }
  2047. #ifdef AUTOCMD
  2048.     --autocmd_no_enter;
  2049. #endif
  2050.     win_enter(firstwin, FALSE);            /* back to first window */
  2051. #ifdef AUTOCMD
  2052.     --autocmd_no_leave;
  2053. #endif
  2054.  
  2055.     /*
  2056.      * Close superfluous windows.
  2057.      */
  2058.     for (wp = lastwin; open_wins > count; )
  2059.     {
  2060.     if (p_hid || !buf_changed(wp->w_buffer)
  2061.                       || autowrite(wp->w_buffer, FALSE) == OK)
  2062.     {
  2063.         close_window(wp, !p_hid);
  2064.         --open_wins;
  2065.         wp = lastwin;
  2066.     }
  2067.     else
  2068.     {
  2069.         wp = wp->w_prev;
  2070.         if (wp == NULL)
  2071.         break;
  2072.     }
  2073.     }
  2074. }
  2075.  
  2076. /*
  2077.  * do_modelines() - process mode lines for the current file
  2078.  *
  2079.  * Returns immediately if the "ml" option isn't set.
  2080.  */
  2081. static int  chk_modeline __ARGS((linenr_t));
  2082.  
  2083.     void
  2084. do_modelines()
  2085. {
  2086.     linenr_t        lnum;
  2087.     int            nmlines;
  2088.  
  2089.     if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
  2090.     return;
  2091.  
  2092.     for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
  2093.                                        ++lnum)
  2094.     if (chk_modeline(lnum) == FAIL)
  2095.         nmlines = 0;
  2096.  
  2097.     for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines &&
  2098.               lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
  2099.     if (chk_modeline(lnum) == FAIL)
  2100.         nmlines = 0;
  2101. }
  2102.  
  2103. /*
  2104.  * chk_modeline() - check a single line for a mode string
  2105.  * Return FAIL if an error encountered.
  2106.  */
  2107.     static int
  2108. chk_modeline(lnum)
  2109.     linenr_t lnum;
  2110. {
  2111.     char_u        *s;
  2112.     char_u        *e;
  2113.     char_u        *linecopy;        /* local copy of any modeline found */
  2114.     int            prev;
  2115.     int            end;
  2116.     int            retval = OK;
  2117.     char_u        *save_sourcing_name;
  2118.  
  2119.     prev = -1;
  2120.     for (s = ml_get(lnum); *s != NUL; ++s)
  2121.     {
  2122.     if (prev == -1 || vim_isspace(prev))
  2123.     {
  2124.         if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0) ||
  2125.                    STRNCMP(s, "vi:", (size_t)3) == 0 ||
  2126.                    STRNCMP(s, "vim:", (size_t)4) == 0)
  2127.         break;
  2128.     }
  2129.     prev = *s;
  2130.     }
  2131.  
  2132.     if (*s)
  2133.     {
  2134.     do                /* skip over "ex:", "vi:" or "vim:" */
  2135.         ++s;
  2136.     while (s[-1] != ':');
  2137.  
  2138.     s = linecopy = vim_strsave(s);    /* copy the line, it will change */
  2139.     if (linecopy == NULL)
  2140.         return FAIL;
  2141.  
  2142.     sourcing_lnum = lnum;        /* prepare for emsg() */
  2143.     save_sourcing_name = sourcing_name;
  2144.     sourcing_name = (char_u *)"modelines";
  2145.  
  2146.     end = FALSE;
  2147.     while (end == FALSE)
  2148.     {
  2149.         s = skipwhite(s);
  2150.         if (*s == NUL)
  2151.         break;
  2152.  
  2153.         /*
  2154.          * Find end of set command: ':' or end of line.
  2155.          */
  2156.         for (e = s; (*e != ':' || *(e - 1) == '\\') && *e != NUL; ++e)
  2157.         ;
  2158.         if (*e == NUL)
  2159.         end = TRUE;
  2160.  
  2161.         /*
  2162.          * If there is a "set" command, require a terminating ':' and
  2163.          * ignore the stuff after the ':'.
  2164.          * "vi:set opt opt opt: foo" -- foo not interpreted
  2165.          * "vi:opt opt opt: foo" -- foo interpreted
  2166.          */
  2167.         if (STRNCMP(s, "set ", (size_t)4) == 0)
  2168.         {
  2169.         if (*e != ':')        /* no terminating ':'? */
  2170.             break;
  2171.         end = TRUE;
  2172.         s += 4;
  2173.         }
  2174.  
  2175.         *e = NUL;            /* truncate the set command */
  2176.         if (do_set(s) == FAIL)    /* stop if error found */
  2177.         {
  2178.         retval = FAIL;
  2179.         break;
  2180.         }
  2181.         s = e + 1;            /* advance to next part */
  2182.     }
  2183.  
  2184.     sourcing_lnum = 0;
  2185.     sourcing_name = save_sourcing_name;
  2186.  
  2187.     vim_free(linecopy);
  2188.     }
  2189.     return retval;
  2190. }
  2191.  
  2192. #ifdef VIMINFO
  2193.     int
  2194. read_viminfo_bufferlist(line, fp, writing)
  2195.     char_u    *line;
  2196.     FILE    *fp;
  2197.     int        writing;
  2198. {
  2199.     char_u    *tab;
  2200.     linenr_t    lnum;
  2201.     colnr_t    col;
  2202.     BUF        *buf;
  2203.     char_u    *sfname;
  2204.  
  2205.     /* don't read in if there are files on the command-line or if writing: */
  2206.     if (!writing && arg_file_count == 0 && find_viminfo_parameter('%') != NULL)
  2207.     {
  2208.     tab = vim_strchr(line + 1, '\t');
  2209.     lnum = 0;
  2210.     col = 0;
  2211.     if (tab != NULL)
  2212.     {
  2213.         *tab++ = '\0';
  2214.         lnum = atol((char *)tab);
  2215.         tab = vim_strchr(tab, '\t');
  2216.         if (tab != NULL)
  2217.         col = atoi((char *)tab + 1);
  2218.     }
  2219.  
  2220.     /* Expand "~/" in the file name at "line + 1" to a full path.
  2221.      * Then try shortening it by comparing with the current directory */
  2222.     expand_env(line + 1, NameBuff, MAXPATHL);
  2223.     mch_dirname(IObuff, IOSIZE);
  2224.     sfname = shorten_fname(NameBuff, IObuff);
  2225.     if (sfname == NULL)
  2226.         sfname = NameBuff;
  2227.  
  2228.     buf = buflist_new(NameBuff, sfname, (linenr_t)0, FALSE);
  2229.     if (buf != NULL)    /* just in case... */
  2230.     {
  2231.         buf->b_last_cursor.lnum = lnum;
  2232.         buf->b_last_cursor.col = col;
  2233.         buflist_setlnum(buf, lnum, col);
  2234.     }
  2235.     }
  2236.  
  2237.     return vim_fgets(line, LSIZE, fp);
  2238. }
  2239.  
  2240.     void
  2241. write_viminfo_bufferlist(fp)
  2242.     FILE    *fp;
  2243. {
  2244.     BUF        *buf;
  2245.     WIN        *win;
  2246.  
  2247.     if (find_viminfo_parameter('%') == NULL)
  2248.     return;
  2249.  
  2250.     for (win = firstwin; win != NULL; win = win->w_next)
  2251.     set_last_cursor(win);
  2252.  
  2253.     fprintf(fp, "\n# Buffer list:\n");
  2254.     for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
  2255.     {
  2256.     if (buf->b_fname == NULL || buf->b_help || removable(buf->b_ffname))
  2257.         continue;
  2258.  
  2259.     home_replace(NULL, buf->b_ffname, NameBuff, MAXPATHL);
  2260.  
  2261.     fprintf(fp, "%%%s\t%ld\t%d\n", NameBuff,
  2262.             (long)buf->b_last_cursor.lnum,
  2263.             buf->b_last_cursor.col);
  2264.     }
  2265. }
  2266. #endif
  2267.